package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.linlinjava.litemall.db.dao.LitemallUserApproveMapper;
import org.linlinjava.litemall.db.domain.LitemallUserApprove;
import org.linlinjava.litemall.db.domain.LitemallUserApproveExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;

@Service
public class LitemallUserApproveService {

	@Resource
	private LitemallUserApproveMapper userApproveMapper;

	public LitemallUserApprove findById(Integer userId) {
		return userApproveMapper.selectByPrimaryKey(userId);
	}

	public LitemallUserApprove findById(Integer userId, Integer authId) {
		LitemallUserApproveExample example = new LitemallUserApproveExample();
		example.or().andIdEqualTo(authId).andUserIdEqualTo(userId).andDeletedEqualTo(false);
		return userApproveMapper.selectOneByExample(example);
	}

	public void add(LitemallUserApprove user) {
		user.setAddTime(LocalDateTime.now());
		user.setUpdateTime(LocalDateTime.now());
		userApproveMapper.insertSelective(user);
	}

	public int updateById(LitemallUserApprove user) {
		user.setUpdateTime(LocalDateTime.now());
		return userApproveMapper.updateByPrimaryKeySelective(user);
	}

	public int count() {
		LitemallUserApproveExample example = new LitemallUserApproveExample();
		example.or().andDeletedEqualTo(false);

		return (int) userApproveMapper.countByExample(example);
	}

	public List<LitemallUserApprove> queryList(Integer userId, Byte status, Integer page, Integer size, String sort,
			String order) {
		LitemallUserApproveExample example = new LitemallUserApproveExample();
		LitemallUserApproveExample.Criteria criteria = example.or();

		criteria.andUserIdEqualTo(userId);
		if (1 == status) {
			criteria.andApproveStatusEqualTo((byte) 0);
		} else if (2 == status) {
			List<Byte> statusList = new ArrayList<>();
			statusList.add((byte) 1);
			statusList.add((byte) 2);
			criteria.andApproveStatusIn(statusList);
		}

		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}

		if (!StringUtils.isEmpty(page) && !StringUtils.isEmpty(size)) {
			PageHelper.startPage(page, size);
		}

		return userApproveMapper.selectByExample(example);
	}

	public long countByUserIdAndStatus(Integer userId, Byte status) {
		LitemallUserApproveExample example = new LitemallUserApproveExample();
		example.or().andUserIdEqualTo(userId).andApproveStatusEqualTo(status).andDeletedEqualTo(false);
		return userApproveMapper.countByExample(example);
	}

	public long checkByAdminUsername(Integer userId, String adminUsername) {
		LitemallUserApproveExample example = new LitemallUserApproveExample();
		example.or().andApproveStatusEqualTo((byte) 0).andIdNotEqualTo(userId).andAdminUsernameEqualTo(adminUsername)
				.andDeletedEqualTo(false);
		return userApproveMapper.countByExample(example);
	}

	public void deleteById(Integer id) {
		userApproveMapper.logicalDeleteByPrimaryKey(id);
	}
}
